/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package yxt.push.org.androidpn.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Broadcast receiver that handles push notification messages from the server.
 * This should be registered as receiver in AndroidManifest.xml.
 * 
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public final class NotificationReceiver extends BroadcastReceiver
{

	private List<Map<String, Object>> list_type = new ArrayList<Map<String, Object>>();
	private static final String LOGTAG = LogUtil.makeLogTag(NotificationReceiver.class);

	// private NotificationService notificationService;

	public NotificationReceiver()
	{
	}

	// public NotificationReceiver(NotificationService notificationService) {
	// this.notificationService = notificationService;
	// }

	@Override
	public void onReceive(Context context, Intent intent)
	{
		Log.d(LOGTAG, "NotificationReceiver.onReceive()...");
		String action = intent.getAction();
		Log.d(LOGTAG, "action=" + action);

		if (Constants.ACTION_SHOW_NOTIFICATION.equals(action))
		{
			String notificationId = intent.getStringExtra(Constants.NOTIFICATION_ID);
			String notificationApiKey = intent.getStringExtra(Constants.NOTIFICATION_API_KEY);
			String notificationTitle = intent.getStringExtra(Constants.NOTIFICATION_TITLE);
			String notificationMessage = intent.getStringExtra(Constants.NOTIFICATION_MESSAGE);
			String notificationUri = intent.getStringExtra(Constants.NOTIFICATION_URI);
			String notificationType = intent.getStringExtra(Constants.NOTIFICATION_TYPE);
			String notificationRemark = intent.getStringExtra(Constants.NOTIFICATION_REMARK);

			String accountname = intent.getStringExtra(Constants.ACCOUNTNAME);
			String mobilephone = intent.getStringExtra(Constants.MOBILEPHONE);
			String hospitalcode = intent.getStringExtra(Constants.HOSPITALCODE);
			String senddate = intent.getStringExtra(Constants.SENDDATE);
			int i = 0;
			for (i = 0; i < list_type.size(); i++)
			{
				Map<String, Object> m = list_type.get(i);
				if (m.get("type").equals(notificationType))
					break;
			}
			if (i == list_type.size())
			{
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", notificationType);
				list_type.add(map);
			}

			Log.d(LOGTAG, "notificationId=" + notificationId);
			Log.d(LOGTAG, "notificationApiKey=" + notificationApiKey);
			Log.d(LOGTAG, "notificationTitle=" + notificationTitle);
			Log.d(LOGTAG, "notificationMessage=" + notificationMessage);
			Log.d(LOGTAG, "notificationUri=" + notificationUri);

			Notifier notifier = new Notifier(context);
			notifier.notify(notificationId, notificationApiKey, notificationTitle, notificationMessage, notificationUri,
					notificationRemark, notificationType, list_type, accountname, mobilephone,hospitalcode,senddate);
		}

		// } else if (Constants.ACTION_NOTIFICATION_CLICKED.equals(action)) {
		// String notificationId = intent
		// .getStringExtra(Constants.NOTIFICATION_ID);
		// String notificationApiKey = intent
		// .getStringExtra(Constants.NOTIFICATION_API_KEY);
		// String notificationTitle = intent
		// .getStringExtra(Constants.NOTIFICATION_TITLE);
		// String notificationMessage = intent
		// .getStringExtra(Constants.NOTIFICATION_MESSAGE);
		// String notificationUri = intent
		// .getStringExtra(Constants.NOTIFICATION_URI);
		//
		// Log.e(LOGTAG, "notificationId=" + notificationId);
		// Log.e(LOGTAG, "notificationApiKey=" + notificationApiKey);
		// Log.e(LOGTAG, "notificationTitle=" + notificationTitle);
		// Log.e(LOGTAG, "notificationMessage=" + notificationMessage);
		// Log.e(LOGTAG, "notificationUri=" + notificationUri);
		//
		// Intent detailsIntent = new Intent();
		// detailsIntent.setClass(context, NotificationDetailsActivity.class);
		// detailsIntent.putExtras(intent.getExtras());
		// // detailsIntent.putExtra(Constants.NOTIFICATION_ID, notificationId);
		// // detailsIntent.putExtra(Constants.NOTIFICATION_API_KEY,
		// notificationApiKey);
		// // detailsIntent.putExtra(Constants.NOTIFICATION_TITLE,
		// notificationTitle);
		// // detailsIntent.putExtra(Constants.NOTIFICATION_MESSAGE,
		// notificationMessage);
		// // detailsIntent.putExtra(Constants.NOTIFICATION_URI, notificationUri);
		// detailsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// detailsIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
		//
		// try {
		// context.startActivity(detailsIntent);
		// } catch (ActivityNotFoundException e) {
		// Toast toast = Toast.makeText(context,
		// "No app found to handle this request",
		// Toast.LENGTH_LONG);
		// toast.show();
		// }
		//
		// } else if (Constants.ACTION_NOTIFICATION_CLEARED.equals(action)) {
		// //
		// }

	}

}
